/**
 * Created by fupeng on 17/3/9.
 */
var pool = require('./DBConnectPool');
var logger = require('../../common/Logger/Logger');

module.exports = {
    addMovieCollectionByMemberidAndMid :  function (_param,callBack) {
        pool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                console.log('Error: get connection from pool in member_movie_collection: ', err);
                return;
            }

            var sql = 'insert into  member_movie_collection(member_id,mid) values(?,?)';

            connection.query(sql,_param ,function (queryErr, rows) {
                if (queryErr) {

                    console.log('insert into  member_movie_collection failed: ', queryErr);

                    callBack({
                        isSuccess: false
                    });

                } else {

                    callBack({
                        isSuccess: true,
                        rows: rows
                    })

                }
                connection.release();

            })
        })
    },
    selectMovieCollectionByMemberidAndMid:function(_param,callBack){
        pool.getPoolConnection('mtsc', function (err, connection) {

            if (err) {
                console.log('Error: get connection from pool in member_movie_collection: ', err);
                return;
            }

            var sql = 'select * from member_movie_collection as movie where member_id = ? and mid = ?';

            connection.query(sql,_param ,function (queryErr, rows) {
                if (queryErr) {

                    console.log('select * from member_movie_collection ', queryErr);

                    callBack({
                        isSuccess: false
                    });

                } else {
                    callBack({
                        isSuccess: true,
                        rows: rows
                    })

                }
                connection.release();

            })
        })

    },
    deleteMovieCollectionByMemberidAndMid:function(_param,callBack){
        pool.getPoolConnection('mtsc', function (err, connection) {

            if (err) {
                console.log('Error: get connection from pool in member_movie_collection: ', err);
                return;
            }

            var sql = 'delete from member_movie_collection where member_id = ? and mid = ?';

            connection.query(sql,_param ,function (queryErr, rows) {
                if (queryErr) {

                    console.log('delete * from member_cinema_collection where member_id = ? and mid = ?', queryErr);

                    callBack({
                        isSuccess: false
                    });

                } else {

                    callBack({
                        isSuccess: true
                    })

                }
                connection.release();

            })
        })
    },
    getMovie :function (param,callBack){
        pool.getPoolConnection('mtsc', function (err, connection) {

            if (err) {
                console.log('get connection from pool failed in movie: ', err);
                return;
            }
            var sql = 'SELECT movie.poster,movie.mid,movie.movie_name,movie.director,movie.brief,movie.cast,movie.score,movie.country,movie.category,date_format(movie.publishdate, "%Y") as publishdate,movie.movie_bigpic  from movie inner join member_movie_collection c on c.mid=movie.mid and c.member_id = ?';
            if(param.movie_name){
                sql += ' and movie_name like "%'+ param.movie_name +'%" ';
            }
             sql += ' order by c.createtime desc LIMIT ?,10';
            connection.query(sql, [param.member_id,param.lastIndex],function (queryErr, rows) {

                if (queryErr) {
                    console.log('select * from movie failed: ', queryErr);
                    callBack({
                        isSuccess: false
                    });
                } else {
                    callBack({
                        isSuccess: true,
                        rows: rows
                    })
                }
                connection.release();
            });

        });
    }
};